package proxy;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName ProxyExample
 * @PackageName proxy
 * @projectName myThread
 * @Description 静态代理示例
 * @Author games
 * @Date 2025/8/29 下午1:16
 * @Version 1.0
 */

@Slf4j
public class StaticProxy {
    // 接口定义
    interface UserService {
        void addUser(String username);

        String getUser(int id);
    }

    // 真实主题类
    static class UserServiceImpl implements UserService {
        @Override
        public void addUser(String username) {
            log.info("添加用户: {}", username);
        }

        @Override
        public String getUser(int id) {
            log.info("获取用户信息，用户ID: {}", id);
            return "用户ID: " + id;
        }
    }

    // 代理类
    static class UserServiceProxy implements UserService {
        private UserService userService;

        public UserServiceProxy(UserService userService) {
            this.userService = userService;
        }

        @Override
        public void addUser(String username) {
            log.info("开始添加用户...");
            userService.addUser(username);
            log.info("用户添加完成");
        }

        @Override
        public String getUser(int id) {
            log.info("开始获取用户信息...");
            String result = userService.getUser(id);
            log.info("用户信息获取完成");
            return result;
        }
    }

    // 使用示例
    public static void main(String[] args) {
        log.info("开始执行静态代理示例");
        StaticProxy.UserService userService = new StaticProxy.UserServiceImpl();
        StaticProxy.UserService proxy = new StaticProxy.UserServiceProxy(userService);
        log.info("用户添加完成");
        proxy.addUser("张三");
        proxy.getUser(1);
        log.info("静态代理示例执行完成");
    }

}